<template>
  <div class="article-manager">
    <!-- <el-card :bordered="false" shadow="never" class="ivu-mt" :body-style="{ padding: 0 }">
      <div class="padding-add">
        <el-form ref="artFrom" :model="artFrom" inline label-width="80px" label-position="right" @submit.native.prevent>
          <el-form-item label="商品分类：" prop="pid" label-for="pid">
            <el-select
              v-model="artFrom.pid"
              placeholder="请选择商品分类"
              @change="userSearchs"
              clearable
              class="form_content_width"
            >
              <el-option v-for="item in treeSelect" :value="item.id" :label="item.cate_name" :key="item.id">{{
                item.cate_name
              }}</el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类状态：" label-for="is_show">
            <el-select
              v-model="artFrom.is_show"
              placeholder="请选择分类状态"
              clearable
              @change="userSearchs"
              class="form_content_width"
            >
              <el-option value="1" label="显示"></el-option>
              <el-option value="0" label="隐藏"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称：" label-for="status2">
            <el-input clearable placeholder="请输入分类名称" v-model="artFrom.cate_name" class="form_content_width" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-db-click @click="userSearchs">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card> -->
    <el-card :bordered="false" shadow="never" class="ivu-mt mt16">
		
		<!-- <el-tabs v-model="artFrom.type" @tab-click="onClickTab">
		  <el-tab-pane
		    :label="item.cate_name"
		    :name="item.id"
		    v-for="(item, index) in headeNum"
		    :key="index"
		  />
		</el-tabs> -->
		
		<el-tabs v-model="artFrom.cate_id" @tab-click="handleClick">
		    <el-tab-pane v-for="item in headeNum" :label="item.cate_name" :name="item.id"></el-tab-pane>
		</el-tabs>
		
		
      <!-- <el-button v-auth="['product-save-cate']" type="primary" class="bnt" v-db-click @click="addClass">添加课程</el-button> -->
	  
	  <router-link v-auth="['admin-course-addcourse']" :to="$routeProStr + '/course/add_course'"><el-button type="primary" class="mr14">添加课程</el-button></router-link >
	  
      <vxe-table
        class="mt14"
        highlight-hover-row
        :loading="loading"
        header-row-class-name="false"
        :tree-config="{ children: 'children' }"
        :data="tableData"
      >
        <vxe-table-column field="id" title="ID" tooltip width="80"></vxe-table-column>
        <vxe-table-column field="course_name" tree-node title="课程名称/章节名称" min-width="380"></vxe-table-column>
        <!-- <vxe-table-column field="chapter"  title="课程章节" min-width="100"></vxe-table-column> -->
        <!-- <vxe-table-column field="teacher"  title="主讲老师" min-width="100"></vxe-table-column> -->
        <vxe-table-column field="is_show" title="是否显示" min-width="80">
          <template v-slot="{ row }">
				<div v-if="row.is_show == 1">显示</div>
				<div v-else style="color: red;font-weight: 700;">隐藏</div>
          </template>
        </vxe-table-column>
        <vxe-table-column field="sort" title="排序" min-width="80" tooltip="true"></vxe-table-column>
        <vxe-table-column field="create_time" title="添加时间" min-width="100" tooltip="true"></vxe-table-column>
     <!--   <vxe-table-column field="is_show" title="状态" min-width="120">
          <template v-slot="{ row }">
            <el-switch
              class="defineSwitch"
              :active-value="1"
              :inactive-value="0"
              v-model="row.is_show"
              :value="row.is_show"
              @change="onchangeIsShow(row)"
              size="large"
              active-text="开启"
              inactive-text="关闭"
            >
            </el-switch>
          </template>
        </vxe-table-column> -->
        <vxe-table-column field="date" title="操作" width="300" fixed="right">
          <template v-slot="{ row, index }">
			  <a v-if="row.is_top == 1"  @click="addchaptea(row)">添加章节</a>
			  
			  <el-divider v-if="row.is_top == 1" direction="vertical"></el-divider>
            <a v-db-click v-if="row.is_top == 1" @click="edit(row)">编辑</a>
			
			
			<a v-db-click v-if="row.is_top != 1" @click="shitilist(row)">试题列表</a>
			<el-divider v-if="row.is_top != 1" direction="vertical"></el-divider>
			
			<a v-db-click v-if="row.is_top != 1" @click="editchaptea(row)">编辑章节</a>
			
			
            <el-divider direction="vertical"></el-divider>
            <a v-db-click v-if="row.is_top == 1" @click="del(row, '删除课程', index)">删除</a>
			<a v-db-click v-else @click="chapterdel(row, '删除章节', index)">删除章节</a>
			
			<el-divider direction="vertical"></el-divider>
			<a v-db-click v-if="row.is_top != 1" @click="copyCreate(row)">删除并新建</a>
			
          </template>
        </vxe-table-column>
      </vxe-table>
    </el-card>
    <!-- 添加 编辑表单-->
    <edit-from ref="edits" :FromData="FromData" @submitFail="userSearchs"></edit-from>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { courselistApi, courseCateCreateApi, courseCateEditApi, setShowApi, treeListApi ,getcoursecatelist } from '@/api/product';
import editFrom from '../../../components/from/from';
export default {
  name: 'product_productClassify',
  components: {
    editFrom,
  },
  data() {
    return {
      treeSelect: [],
      FromData: null,
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      loading: false,
      artFrom: {
        pid: 0,
        is_show: '',
        page: 1,
        cate_name: '',
        limit: 15,
		type: 9,
		cate_id:0
      },
	  
	  activeName:'',
	  
      total: 0,
      tableData: [],
	  
	  headeNum:[],
	  
    };
  },
  computed: {
    ...mapState('admin/userLevel', ['categoryId']),
  },
  mounted() {
	  this.goodHeade();
    // this.goodsCategory();
  
  },
  

  
  methods: {
	  
	handleClick(tab, event) {
		console.log(this.artFrom.cate_id);
		this.artFrom.page = 1;
		this.getList();
	},  
	  
	//选择头部  
	onClickTab(){
		this.$refs.table.clearSelection();
		console.log('ttttttttttttttttttttttttttttttttttttttt');
		console.log(this.artFrom.type);
		console.log('ttttttttttttttttttttttttttttttttttttttt');
	},
	
	// 获取商品表单头数量
	goodHeade() {
	  getcoursecatelist()
	    .then((res) => {
	       this.headeNum = res.data;
		   this.artFrom.cate_id = res.data[0].id
		   this.getList();
	    })
	    .catch((res) => {
	      this.$message.error(res.msg);
	    });
	},  
	  
	  
    // 商品分类；
    goodsCategory() {
      treeListApi(0)
        .then((res) => {
          this.treeSelect = res.data;
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    // 列表
    getList() {
      this.loading = true;
      this.artFrom.is_show = this.artFrom.is_show || '';
      this.artFrom.pid = this.artFrom.pid || '';
      courselistApi(this.artFrom)
        .then(async (res) => {
          let data = res.data;
          this.tableData = data.list;
          this.total = data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$message.error(res.msg);
        });
    },
    pageChange(index) {
      this.artFrom.page = index;
      this.getList();
    },
    // 添加
    addClass() {
      this.$modalForm(courseCateCreateApi()).then(() => this.getList());
    },
	
    // 编辑
    edit(row) {
      this.$router.push({ path: this.$routeProStr + '/course/add_course/' + row.id });
    },
	
	//试题列表
	shitilist(row){
		this.$router.push({ path: this.$routeProStr + '/course/add_questions/' + row.id });
	},
	
	//添加章节
	addchaptea(row){
		var newid = 'add_'+row.id
		this.$router.push({ path: this.$routeProStr + '/course/add_chapter/' + newid });
	},
	
	//复制和新建课程
	copyCreate(row){
		var newid = 'copycreate_'+row.id+'_'+row.sort+'_'+row.course_name
		this.$router.push({ path: this.$routeProStr + '/course/add_chapter/' + newid });
	},
	
	//编辑章节
	editchaptea(row){
		var newid = 'edit_'+row.id
		this.$router.push({ path: this.$routeProStr + '/course/add_chapter/' + newid });
	},
	
	
    // 修改状态
    onchangeIsShow(row) {
      let data = {
        id: row.id,
        is_show: row.is_show,
      };
      setShowApi(data)
        .then(async (res) => {
          this.$message.success(res.msg);
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    // 下拉树
    handleCheckChange(data) {
      let value = '';
      let title = '';
      this.list = [];
      this.artFrom.pid = 0;
      data.forEach((item, index) => {
        value += `${item.id},`;
        title += `${item.title},`;
      });
      value = value.substring(0, value.length - 1);
      title = title.substring(0, title.length - 1);
      this.list.push({
        value,
        title,
      });
      this.artFrom.pid = value;
      this.getList();
    },
	
	
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `product/courselistdelete/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$message.success(res.msg);
          this.getList();
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
	
	chapterdel(row, tit, num) {
	  let delfromData = {
	    title: tit,
	    num: num,
	    url: `product/chapterdelete/${row.id}`,
	    method: 'DELETE',
	    ids: '',
	  };
	  this.$modalSure(delfromData)
	    .then((res) => {
	      this.$message.success(res.msg);
	      this.getList();
	    })
	    .catch((res) => {
	      this.$message.error(res.msg);
	    });
	},
	
	
	
    // 表格搜索
    userSearchs() {
      this.artFrom.page = 1;
      this.getList();
    },
  },
};
</script>
<style scoped lang="stylus">
.treeSel ::v-deep .ivu-select-dropdown-list {
  padding: 0 10px !important;
  box-sizing: border-box;
}

.tabBox_img {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
